home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / filutil / bison110.zip / OUTPUT.C < prev    next >
C/C++ Source or Header  |  1990-06-26  |  27KB  |  1,382 lines

  1. /* Output the generated parsing program for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* functions to output parsing data to various files.  Entries are:
  22.  
  23.   output_headers ()
  24.  
  25. Output constant strings to the beginning of certain files.
  26.  
  27.   output_trailers()
  28.  
  29. Output constant strings to the ends of certain files.
  30.  
  31.   output ()
  32.  
  33. Output the parsing tables and the parser code to ftable.
  34.  
  35. The parser tables consist of:  (starred ones needed only for the semantic parser)
  36.  
  37. yytranslate = vector mapping yylex's token numbers into bison's token numbers.
  38.  
  39. yytname = vector of string-names indexed by bison token number
  40.  
  41. yyrline = vector of line-numbers of all rules.  For yydebug printouts.
  42.  
  43. * yyrhs = vector of items of all rules.
  44.         This is exactly what ritems contains.
  45.  
  46. * yyprhs[r] = index in yyrhs of first item for rule r.
  47.  
  48. yyr1[r] = symbol number of symbol that rule r derives.
  49.  
  50. yyr2[r] = number of symbols composing right hand side of rule r.
  51.  
  52. * yystos[s] = the symbol number of the symbol that leads to state s.
  53.  
  54. yydefact[s] = default rule to reduce with in state s,
  55.           when yytable doesn't specify something else to do.
  56.           Zero means the default is an error.
  57.  
  58. yydefgoto[i] = default state to go to after a reduction of a rule that
  59.            generates variable ntokens + i, except when yytable
  60.            specifies something else to do.
  61.  
  62. yypact[s] = index in yytable of the portion describing state s.
  63.             The lookahed token's type is used to index that portion
  64.             to find out what to do.
  65.  
  66.         If the value in yytable is positive,
  67.         we shift the token and go to that state.
  68.  
  69.         If the value is negative, it is minus a rule number to reduce by.
  70.  
  71.         If the value is zero, the default action from yydefact[s] is used.
  72.  
  73. yypgoto[i] = the index in yytable of the portion describing
  74.              what to do after reducing a rule that derives variable i + ntokens.
  75.              This portion is indexed by the parser state number
  76.          as of before the text for this nonterminal was read.
  77.          The value from yytable is the state to go to.
  78.  
  79. yytable = a vector filled with portions for different uses,
  80.           found via yypact and yypgoto.
  81.  
  82. yycheck = a vector indexed in parallel with yytable.
  83.       It indicates, in a roundabout way, the bounds of the
  84.       portion you are trying to examine.
  85.  
  86.       Suppose that the portion of yytable starts at index p
  87.       and the index to be examined within the portion is i.
  88.       Then if yycheck[p+i] != i, i is outside the bounds
  89.       of what is actually allocated, and the default
  90.       (from yydefact or yydefgoto) should be used.
  91.       Otherwise, yytable[p+i] should be used.
  92.  
  93. YYFINAL = the state number of the termination state.
  94. YYFLAG = most negative short int.  Used to flag ??
  95. YYNTBASE = ntokens.
  96.  
  97. */
  98.  
  99. #include <stdio.h>
  100. #include "system.h"
  101. #include "machine.h"
  102. #include "new.h"
  103. #include "files.h"
  104. #include "gram.h"
  105. #include "state.h"
  106.  
  107.  
  108. extern int debugflag;
  109. extern int nolinesflag;
  110.  
  111. extern char **tags;
  112. extern int tokensetsize;
  113. extern int final_state;
  114. extern core **state_table;
  115. extern shifts **shift_table;
  116. extern errs **err_table;
  117. extern reductions **reduction_table;
  118. extern short *accessing_symbol;
  119. extern unsigned *LA;
  120. extern short *LAruleno;
  121. extern short *lookaheads;
  122. extern char *consistent;
  123. extern short *goto_map;
  124. extern short *from_state;
  125. extern short *to_state;
  126.  
  127. void output_token_translations();
  128. void output_gram();
  129. void output_stos();
  130. void output_rule_data();
  131. void output_defines();
  132. void output_actions();
  133. void token_actions();
  134. void save_row();
  135. void goto_actions();
  136. void save_column();
  137. void sort_actions();
  138. void pack_table();
  139. void output_base();
  140. void output_table();
  141. void output_check();
  142. void output_parser();
  143. void output_program();
  144. void free_itemset();
  145. void free_shifts();
  146. void free_reductions();
  147. void free_itemsets();
  148. int action_row();
  149. int default_goto();
  150. int matching_state();
  151. int pack_vector();
  152.  
  153. extern void berror();
  154. extern void fatals();
  155.  
  156. static int nvectors;
  157. static int nentries;
  158. static short **froms;
  159. static short **tos;
  160. static short *tally;
  161. static short *width;
  162. static short *actrow;
  163. static short *state_count;
  164. static short *order;
  165. static short *base;
  166. static short *pos;
  167. static short *table;
  168. static short *check;
  169. static int lowzero;
  170. static int high;
  171.  
  172.  
  173.  
  174. #define    GUARDSTR    "\n#include \"%s\"\nextern int yyerror;\n\
  175. extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
  176. yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  177. register YYLTYPE *yylsp;\n\
  178. {\n  yyerror = 0;\nyycost = 0;\n  yymsg = 0;\nswitch (n)\n    {"
  179.  
  180. #define    ACTSTR        "\n#include \"%s\"\nextern YYSTYPE yyval;\
  181. \nextern int yychar;\
  182. yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  183. register YYLTYPE *yylsp;\n{\n  switch (n)\n{"
  184.  
  185. #define    ACTSTR_SIMPLE    "\n  switch (yyn) {\n"
  186.  
  187.  
  188. void
  189. output_headers()
  190. {
  191.   if (semantic_parser)
  192.     fprintf(fguard, GUARDSTR, attrsfile);
  193.   fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
  194. /*  if (semantic_parser)    JF moved this below
  195.     fprintf(ftable, "#include \"%s\"\n", attrsfile);
  196.   fprintf(ftable, "#include <stdio.h>\n\n"); */
  197. }
  198.  
  199.  
  200. void
  201. output_trailers()
  202. {
  203.   if (semantic_parser)
  204.     {
  205.       fprintf(fguard, "\n    }\n}\n");
  206.       fprintf(faction, "\n    }\n}\n");
  207.     }
  208.   else
  209.     fprintf(faction, "\n}\n");
  210. }
  211.  
  212.  
  213. void
  214. output()
  215. {
  216.   int c;
  217.  
  218.   /* output_token_defines(ftable);    / * JF put out token defines FIRST */
  219.   if (!semantic_parser)        /* JF Put out other stuff */
  220.     {
  221.       rewind(fattrs);
  222.       while ((c=getc(fattrs))!=EOF)
  223.         putc(c,ftable);
  224.     }
  225.  
  226.   if (debugflag)
  227.     fprintf(ftable, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n\n",
  228.         !!debugflag);
  229.  
  230.   if (semantic_parser)
  231.     fprintf(ftable, "#include \"%s\"\n", attrsfile);
  232.   fprintf(ftable, "#include <stdio.h>\n\n");
  233.  
  234.   /* Make "const" do nothing if not in ANSI C.  */
  235.   fprintf (ftable, "#ifndef __STDC__\n#define const\n#endif\n\n");
  236.  
  237.   free_itemsets();
  238.   output_defines();
  239.   output_token_translations();
  240.   if (semantic_parser)
  241.     output_gram();
  242.   FREE(ritem);
  243.   if (semantic_parser)
  244.     output_stos();
  245.   output_rule_data();
  246.   output_actions();
  247.   output_parser();
  248.   output_program();
  249. }
  250.  
  251.  
  252. void
  253. output_token_translations()
  254. {
  255.   register int i, j;
  256. /*   register short *sp; JF unused */
  257.  
  258.   if (translations)
  259.     {
  260.       fprintf(ftable,
  261.           "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
  262.           max_user_token_number, nsyms);
  263.  
  264.       if (ntokens < 127)  /* play it very safe; check maximum element value.  */
  265.         fprintf(ftable, "\nstatic const char yytranslate[] = {     0");
  266.       else
  267.     fprintf(ftable, "\nstatic const short yytranslate[] = {     0");
  268.  
  269.       j = 10;
  270.       for (i = 1; i <= max_user_token_number; i++)
  271.     {
  272.       putc(',', ftable);
  273.  
  274.       if (j >= 10)
  275.         {
  276.           putc('\n', ftable);
  277.           j = 1;
  278.         }
  279.       else
  280.         {
  281.           j++;
  282.         }
  283.  
  284.       fprintf(ftable, "%6d", token_translations[i]);
  285.     }
  286.  
  287.       fprintf(ftable, "\n};\n");
  288.     }
  289.   else
  290.     {
  291.       fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
  292.     }
  293. }
  294.  
  295.  
  296. void
  297. output_gram()
  298. {
  299.   register int i;
  300.   register int j;
  301.   register short *sp;
  302.  
  303.   fprintf(ftable, "\nstatic const short yyprhs[] = {     0");
  304.  
  305.   j = 10;
  306.   for (i = 1; i <= nrules; i++)
  307.     {
  308.       putc(',', ftable);
  309.  
  310.       if (j >= 10)
  311.     {
  312.       putc('\n', ftable);
  313.       j = 1;
  314.     }
  315.       else
  316.     {
  317.       j++;
  318.     }
  319.  
  320.       fprintf(ftable, "%6d", rrhs[i]);
  321.     }
  322.  
  323.   fprintf(ftable, "\n};\n\nstatic const short yyrhs[] = {%6d", ritem[0]);
  324.  
  325.   j = 10;
  326.   for (sp = ritem + 1; *sp; sp++)
  327.     {
  328.       putc(',', ftable);
  329.  
  330.       if (j >= 10)
  331.     {
  332.       putc('\n', ftable);
  333.       j = 1;
  334.     }
  335.       else
  336.     {
  337.       j++;
  338.     }
  339.  
  340.       if (*sp > 0)
  341.     fprintf(ftable, "%6d", *sp);
  342.       else
  343.     fprintf(ftable, "     0");
  344.     }
  345.  
  346.   fprintf(ftable, "\n};\n");
  347. }
  348.  
  349.  
  350. void
  351. output_stos()
  352. {
  353.   register int i;
  354.   register int j;
  355.  
  356.   fprintf(ftable, "\nstatic const short yystos[] = {     0");
  357.  
  358.   j = 10;
  359.   for (i = 1; i < nstates; i++)
  360.     {
  361.       putc(',', ftable);
  362.  
  363.       if (j >= 10)
  364.     {
  365.       putc('\n', ftable);
  366.       j = 1;
  367.     }
  368.       else
  369.     {
  370.       j++;
  371.     }
  372.  
  373.       fprintf(ftable, "%6d", accessing_symbol[i]);
  374.     }
  375.  
  376.   fprintf(ftable, "\n};\n");
  377. }
  378.  
  379.  
  380. void
  381. output_rule_data()
  382. {
  383.   register int i;
  384.   register int j;
  385.  
  386.   fprintf(ftable, "\n#if YYDEBUG != 0\nstatic const short yyrline[] = { 0");
  387.  
  388.   j = 10;
  389.   for (i = 1; i <= nrules; i++)
  390.     {
  391.       putc(',', ftable);
  392.  
  393.       if (j >= 10)
  394.     {
  395.       putc('\n', ftable);
  396.       j = 1;
  397.     }
  398.       else
  399.     {
  400.       j++;
  401.     }
  402.  
  403.       fprintf(ftable, "%6d", rline[i]);
  404.     }
  405.  
  406.   /* Output the table of token names.  */
  407.  
  408.   fprintf(ftable, "\n};\n\nstatic const char * const yytname[] = {     0");
  409.  
  410.   j = 10;
  411.   for (i = 1; i <= ntokens; i++)
  412.     {
  413.       register char *p;
  414.       putc(',', ftable);
  415.  
  416.       if (j >= 10)
  417.     {
  418.       putc('\n', ftable);
  419.       j = 1;
  420.     }
  421.       else
  422.     {
  423.       j++;
  424.     }
  425.  
  426.       putc ('\"', ftable);
  427.  
  428.       for (p = tags[i]; *p; p++)
  429.     if (*p == '"' || *p == '\\')
  430.       fprintf(ftable, "\\%c", *p);
  431.     else if (*p == '\n')
  432.       fprintf(ftable, "\\n");
  433.     else if (*p == '\t')
  434.       fprintf(ftable, "\\t");
  435.     else if (*p == '\b')
  436.       fprintf(ftable, "\\b");
  437.     else if (*p < 040 || *p >= 0177)
  438.       fprintf(ftable, "\\%03o", *p);
  439.     else
  440.       putc(*p, ftable);
  441.  
  442.       putc ('\"', ftable);
  443.     }
  444.  
  445.   fprintf(ftable, "\n};\n#endif\n\nstatic const short yyr1[] = {     0");
  446.  
  447.   j = 10;
  448.   for (i = 1; i <= nrules; i++)
  449.     {
  450.       putc(',', ftable);
  451.  
  452.       if (j >= 10)
  453.     {
  454.       putc('\n', ftable);
  455.       j = 1;
  456.     }
  457.       else
  458.     {
  459.       j++;
  460.     }
  461.  
  462.       fprintf(ftable, "%6d", rlhs[i]);
  463.     }
  464.  
  465.   FREE(rlhs + 1);
  466.  
  467.   fprintf(ftable, "\n};\n\nstatic const short yyr2[] = {     0");
  468.  
  469.   j = 10;
  470.   for (i = 1; i < nrules; i++)
  471.     {
  472.       putc(',', ftable);
  473.  
  474.       if (j >= 10)
  475.     {
  476.       putc('\n', ftable);
  477.       j = 1;
  478.     }
  479.       else
  480.     {
  481.       j++;
  482.     }
  483.  
  484.       fprintf(ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1);
  485.     }
  486.  
  487.   putc(',', ftable);
  488.   if (j >= 10)
  489.     putc('\n', ftable);
  490.  
  491.   fprintf(ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
  492.   FREE(rrhs + 1);
  493. }
  494.  
  495.  
  496. void
  497. output_defines()
  498. {
  499.   fprintf(ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
  500.   fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT);
  501.   fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens);
  502. }
  503.  
  504.  
  505.  
  506. /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck.  */
  507.  
  508. void
  509. output_actions()
  510. {
  511.   nvectors = nstates + nvars;
  512.  
  513.   froms = NEW2(nvectors, short *);
  514.   tos = NEW2(nvectors, short *);
  515.   tally = NEW2(nvectors, short);
  516.   width = NEW2(nvectors, short);
  517.  
  518.   token_actions();
  519.   free_shifts();
  520.   free_reductions();
  521.   FREE(lookaheads);
  522.   FREE(LA);
  523.   FREE(LAruleno);
  524.   FREE(accessing_symbol);
  525.  
  526.   goto_actions();
  527.   FREE(goto_map + ntokens);
  528.   FREE(from_state);
  529.   FREE(to_state);
  530.  
  531.   sort_actions();
  532.   pack_table();
  533.   output_base();
  534.   output_table();
  535.   output_check();
  536. }
  537.  
  538.  
  539.  
  540. /* figure out the actions for the specified state, indexed by lookahead token type.
  541.  
  542.    The yydefact table is output now.  The detailed info
  543.    is saved for putting into yytable later.  */
  544.  
  545. void
  546. token_actions()
  547. {
  548.   register int i;
  549.   register int j;
  550.   register int k;
  551.  
  552.   actrow = NEW2(ntokens, short);
  553.  
  554.   k = action_row(0);
  555.   fprintf(ftable, "\nstatic const short yydefact[] = {%6d", k);
  556.   save_row(0);
  557.  
  558.   j = 10;
  559.   for (i = 1; i < nstates; i++)
  560.     {
  561.       putc(',', ftable);
  562.  
  563.       if (j >= 10)
  564.     {
  565.       putc('\n', ftable);
  566.       j = 1;
  567.     }
  568.       else
  569.     {
  570.       j++;
  571.     }
  572.  
  573.       k = action_row(i);
  574.       fprintf(ftable, "%6d", k);
  575.       save_row(i);
  576.     }
  577.  
  578.   fprintf(ftable, "\n};\n");
  579.   FREE(actrow);
  580. }
  581.  
  582.  
  583.  
  584. /* Decide what to do for each type of token if seen as the lookahead token in specified state.
  585.    The value returned is used as the default action (yydefact) for the state.
  586.    In addition, actrow is filled with what to do for each kind of token,
  587.    index by symbol number, with zero meaning do the default action.
  588.    The value MINSHORT, a very negative number, means this situation
  589.    is an error.  The parser recognizes this value specially.
  590.  
  591.    This is where conflicts are resolved.  The loop over lookahead rules
  592.    considered lower-numbered rules last, and the last rule considered that likes
  593.    a token gets to handle it.  */
  594.  
  595. int
  596. action_row(state)
  597. int state;
  598. {
  599.   register int i;
  600.   register int j;
  601.   register int k;
  602.   register int m;
  603.   register int n;
  604.   register int count;
  605.   register int default_rule;
  606.   register int nreds;
  607.   register int max;
  608.   register int rule;
  609.   register int shift_state;
  610.   register int symbol;
  611.   register unsigned mask;
  612.   register unsigned *wordp;
  613.   register reductions *redp;
  614.   register shifts *shiftp;
  615.   register errs *errp;
  616.   int nodefault = 0;  /* set nonzero to inhibit having any default reduction */
  617.  
  618.   for (i = 0; i < ntokens; i++)
  619.     actrow[i] = 0;
  620.  
  621.   default_rule = 0;
  622.   nreds = 0;
  623.   redp = reduction_table[state];
  624.  
  625.   if (redp)
  626.     {
  627.       nreds = redp->nreds;
  628.  
  629.       if (nreds >= 1)
  630.     {
  631.       /* loop over all the rules available here which require lookahead */
  632.       m = lookaheads[state];
  633.       n = lookaheads[state + 1];
  634.  
  635.       for (i = n - 1; i >= m; i--)
  636.         {
  637.           rule = - LAruleno[i];
  638.           wordp = LA + i * tokensetsize;
  639.           mask = 1;
  640.  
  641.           /* and find each token which the rule finds acceptable to come next */
  642.           for (j = 0; j < ntokens; j++)
  643.         {
  644.           /* and record this rule as the rule to use if that token follows.  */
  645.           if (mask & *wordp)
  646.             actrow[j] = rule;
  647.  
  648.           mask <<= 1;
  649.           if (mask == 0)
  650.             {
  651.               mask = 1;
  652.               wordp++;
  653.             }
  654.         }
  655.         }
  656.     }
  657.     }
  658.  
  659.   shiftp = shift_table[state];
  660.  
  661.   /* now see which tokens are allowed for shifts in this state.
  662.      For them, record the shift as the thing to do.  So shift is preferred to reduce.  */
  663.  
  664.   if (shiftp)
  665.     {
  666.       k = shiftp->nshifts;
  667.  
  668.       for (i = 0; i < k; i++)
  669.     {
  670.       shift_state = shiftp->shifts[i];
  671.       if (! shift_state) continue;
  672.  
  673.       symbol = accessing_symbol[shift_state];
  674.  
  675.       if (ISVAR(symbol))
  676.         break;
  677.  
  678.       actrow[symbol] = shift_state;
  679.  
  680.       /* do not use any default reduction if there is a shift for error */
  681.  
  682.       if (symbol == error_token_number) nodefault = 1;
  683.     }
  684.     }
  685.  
  686.   errp = err_table[state];
  687.  
  688.   /* See which tokens are an explicit error in this state
  689.      (due to %nonassoc).  For them, record MINSHORT as the action.  */
  690.  
  691.   if (errp)
  692.     {
  693.       k = errp->nerrs;
  694.  
  695.       for (i = 0; i < k; i++)
  696.     {
  697.       symbol = errp->errs[i];
  698.       actrow[symbol] = MINSHORT;
  699.     }
  700.     }
  701.  
  702.   /* now find the most common reduction and make it the default action for this state.  */
  703.  
  704.   if (nreds >= 1 && ! nodefault)
  705.     {
  706.       if (consistent[state])
  707.     default_rule = redp->rules[0];
  708.       else
  709.     {
  710.       max = 0;
  711.       for (i = m; i < n; i++)
  712.         {
  713.           count = 0;
  714.           rule = - LAruleno[i];
  715.  
  716.           for (j = 0; j < ntokens; j++)
  717.         {
  718.           if (actrow[j] == rule)
  719.             count++;
  720.         }
  721.  
  722.           if (count > max)
  723.         {
  724.           max = count;
  725.           default_rule = rule;
  726.         }
  727.         }
  728.  
  729.       /* actions which match the default are replaced with zero,
  730.          which means "use the default" */
  731.  
  732.       if (max > 0)
  733.         {
  734.           for (j = 0; j < ntokens; j++)
  735.         {
  736.           if (actrow[j] == default_rule)
  737.             actrow[j] = 0;
  738.         }
  739.  
  740.           default_rule = - default_rule;
  741.         }
  742.     }
  743.     }
  744.  
  745.   /* If have no default rule, the default is an error.
  746.      So replace any action which says "error" with "use default".  */
  747.  
  748.   if (default_rule == 0)
  749.     for (j = 0; j < ntokens; j++)
  750.       {
  751.     if (actrow[j] == MINSHORT)
  752.       actrow[j] = 0;
  753.       }
  754.  
  755.   return (default_rule);
  756. }
  757.  
  758.  
  759. void
  760. save_row(state)
  761. int state;
  762. {
  763.   register int i;
  764.   register int count;
  765.   register short *sp;
  766.   register short *sp1;
  767.   register short *sp2;
  768.  
  769.   count = 0;
  770.   for (i = 0; i < ntokens; i++)
  771.     {
  772.       if (actrow[i] != 0)
  773.     count++;
  774.     }
  775.  
  776.   if (count == 0)
  777.     return;
  778.  
  779.   froms[state] = sp1 = sp = NEW2(count, short);
  780.   tos[state] = sp2 = NEW2(count, short);
  781.  
  782.   for (i = 0; i < ntokens; i++)
  783.     {
  784.       if (actrow[i] != 0)
  785.     {
  786.       *sp1++ = i;
  787.       *sp2++ = actrow[i];
  788.     }
  789.     }
  790.  
  791.   tally[state] = count;
  792.   width[state] = sp1[-1] - sp[0] + 1;
  793. }
  794.  
  795.  
  796.  
  797. /* figure out what to do after reducing with each rule,
  798.    depending on the saved state from before the beginning
  799.    of parsing the data that matched this rule.
  800.  
  801.    The yydefgoto table is output now.  The detailed info
  802.    is saved for putting into yytable later.  */
  803.  
  804. void
  805. goto_actions()
  806. {
  807.   register int i;
  808.   register int j;
  809.   register int k;
  810.  
  811.   state_count = NEW2(nstates, short);
  812.  
  813.   k = default_goto(ntokens);
  814.   fprintf(ftable, "\nstatic const short yydefgoto[] = {%6d", k);
  815.   save_column(ntokens, k);
  816.  
  817.   j = 10;
  818.   for (i = ntokens + 1; i < nsyms; i++)
  819.     {
  820.       putc(',', ftable);
  821.  
  822.       if (j >= 10)
  823.     {
  824.       putc('\n', ftable);
  825.       j = 1;
  826.     }
  827.       else
  828.     {
  829.       j++;
  830.     }
  831.  
  832.       k = default_goto(i);
  833.       fprintf(ftable, "%6d", k);
  834.       save_column(i, k);
  835.     }
  836.  
  837.   fprintf(ftable, "\n};\n");
  838.   FREE(state_count);
  839. }
  840.  
  841.  
  842.  
  843. int
  844. default_goto(symbol)
  845. int symbol;
  846. {
  847.   register int i;
  848.   register int m;
  849.   register int n;
  850.   register int default_state;
  851.   register int max;
  852.  
  853.   m = goto_map[symbol];
  854.   n = goto_map[symbol + 1];
  855.  
  856.   if (m == n)
  857.     return (-1);
  858.  
  859.   for (i = 0; i < nstates; i++)
  860.     state_count[i] = 0;
  861.  
  862.   for (i = m; i < n; i++)
  863.     state_count[to_state[i]]++;
  864.  
  865.   max = 0;
  866.   default_state = -1;
  867.  
  868.   for (i = 0; i < nstates; i++)
  869.     {
  870.       if (state_count[i] > max)
  871.     {
  872.       max = state_count[i];
  873.       default_state = i;
  874.     }
  875.     }
  876.  
  877.   return (default_state);
  878. }
  879.  
  880.  
  881. void
  882. save_column(symbol, default_state)
  883. int symbol;
  884. int default_state;
  885. {
  886.   register int i;
  887.   register int m;
  888.   register int n;
  889.   register short *sp;
  890.   register short *sp1;
  891.   register short *sp2;
  892.   register int count;
  893.   register int symno;
  894.  
  895.   m = goto_map[symbol];
  896.   n = goto_map[symbol + 1];
  897.  
  898.   count = 0;
  899.   for (i = m; i < n; i++)
  900.     {
  901.       if (to_state[i] != default_state)
  902.     count++;
  903.     }
  904.  
  905.   if (count == 0)
  906.     return;
  907.  
  908.   symno = symbol - ntokens + nstates;
  909.  
  910.   froms[symno] = sp1 = sp = NEW2(count, short);
  911.   tos[symno] = sp2 = NEW2(count, short);
  912.  
  913.   for (i = m; i < n; i++)
  914.     {
  915.       if (to_state[i] != default_state)
  916.     {
  917.       *sp1++ = from_state[i];
  918.       *sp2++ = to_state[i];
  919.     }
  920.     }
  921.  
  922.   tally[symno] = count;
  923.   width[symno] = sp1[-1] - sp[0] + 1;
  924. }
  925.  
  926.  
  927.  
  928. /* the next few functions decide how to pack
  929.    the actions and gotos information into yytable. */
  930.  
  931. void
  932. sort_actions()
  933. {
  934.   register int i;
  935.   register int j;
  936.   register int k;
  937.   register int t;
  938.   register int w;
  939.  
  940.   order = NEW2(nvectors, short);
  941.   nentries = 0;
  942.  
  943.   for (i = 0; i < nvectors; i++)
  944.     {
  945.       if (tally[i] > 0)
  946.     {
  947.       t = tally[i];
  948.       w = width[i];
  949.       j = nentries - 1;
  950.  
  951.       while (j >= 0 && (width[order[j]] < w))
  952.         j--;
  953.  
  954.       while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
  955.         j--;
  956.  
  957.       for (k = nentries - 1; k > j; k--)
  958.         order[k + 1] = order[k];
  959.  
  960.       order[j + 1] = i;
  961.       nentries++;
  962.     }
  963.     }
  964. }
  965.  
  966.  
  967. void
  968. pack_table()
  969. {
  970.   register int i;
  971.   register int place;
  972.   register int state;
  973.  
  974.   base = NEW2(nvectors, short);
  975.   pos = NEW2(nentries, short);
  976.   table = NEW2(MAXTABLE, short);
  977.   check = NEW2(MAXTABLE, short);
  978.  
  979.   lowzero = 0;
  980.   high = 0;
  981.  
  982.   for (i = 0; i < nvectors; i++)
  983.     base[i] = MINSHORT;
  984.  
  985.   for (i = 0; i < MAXTABLE; i++)
  986.     check[i] = -1;
  987.  
  988.   for (i = 0; i < nentries; i++)
  989.     {
  990.       state = matching_state(i);
  991.  
  992.       if (state < 0)
  993.     place = pack_vector(i);
  994.       else
  995.     place = base[state];
  996.  
  997.       pos[i] = place;
  998.       base[order[i]] = place;
  999.     }
  1000.  
  1001.   for (i = 0; i < nvectors; i++)
  1002.     {
  1003.       if (froms[i])
  1004.     FREE(froms[i]);
  1005.       if (tos[i])
  1006.     FREE(tos[i]);
  1007.     }
  1008.  
  1009.   FREE(froms);
  1010.   FREE(tos);
  1011.   FREE(pos);
  1012. }
  1013.  
  1014.  
  1015.  
  1016. int
  1017. matching_state(vector)
  1018. int vector;
  1019. {
  1020.   register int i;
  1021.   register int j;
  1022.   register int k;
  1023.   register int t;
  1024.   register int w;
  1025.   register int match;
  1026.   register int prev;
  1027.  
  1028.   i = order[vector];
  1029.   if (i >= nstates)
  1030.     return (-1);
  1031.  
  1032.   t = tally[i];
  1033.   w = width[i];
  1034.  
  1035.   for (prev = vector - 1; prev >= 0; prev--)
  1036.     {
  1037.       j = order[prev];
  1038.       if (width[j] != w || tally[j] != t)
  1039.     return (-1);
  1040.  
  1041.       match = 1;
  1042.       for (k = 0; match && k < t; k++)
  1043.     {
  1044.       if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
  1045.         match = 0;
  1046.     }
  1047.  
  1048.       if (match)
  1049.     return (j);
  1050.     }
  1051.  
  1052.   return (-1);
  1053. }
  1054.  
  1055.  
  1056.  
  1057. int
  1058. pack_vector(vector)
  1059. int vector;
  1060. {
  1061.   register int i;
  1062.   register int j;
  1063.   register int k;
  1064.   register int t;
  1065.   register int loc;
  1066.   register int ok;
  1067.   register short *from;
  1068.   register short *to;
  1069.  
  1070.   i = order[vector];
  1071.   t = tally[i];
  1072.  
  1073.   if (t == 0)
  1074.     berror("pack_vector");
  1075.  
  1076.   from = froms[i];
  1077.   to = tos[i];
  1078.  
  1079.   for (j = lowzero - from[0]; j < MAXTABLE; j++)
  1080.     {
  1081.       ok = 1;
  1082.  
  1083.       for (k = 0; ok && k < t; k++)
  1084.     {
  1085.       loc = j + from[k];
  1086.       if (loc > MAXTABLE)
  1087.         fatals("maximum table size (%d) exceeded",MAXTABLE);
  1088.  
  1089.       if (table[loc] != 0)
  1090.         ok = 0;
  1091.     }
  1092.  
  1093.       for (k = 0; ok && k < vector; k++)
  1094.     {
  1095.       if (pos[k] == j)
  1096.         ok = 0;
  1097.     }
  1098.  
  1099.       if (ok)
  1100.     {
  1101.       for (k = 0; k < t; k++)
  1102.         {
  1103.           loc = j + from[k];
  1104.           table[loc] = to[k];
  1105.           check[loc] = from[k];
  1106.         }
  1107.  
  1108.       while (table[lowzero] != 0)
  1109.         lowzero++;
  1110.  
  1111.       if (loc > high)
  1112.         high = loc;
  1113.  
  1114.       return (j);
  1115.     }
  1116.     }
  1117.  
  1118.   berror("pack_vector");
  1119.   return 0;    /* JF keep lint happy */
  1120. }
  1121.  
  1122.  
  1123.  
  1124. /* the following functions output yytable, yycheck
  1125.    and the vectors whose elements index the portion starts */
  1126.  
  1127. void
  1128. output_base()
  1129. {
  1130.   register int i;
  1131.   register int j;
  1132.  
  1133.   fprintf(ftable, "\nstatic const short yypact[] = {%6d", base[0]);
  1134.  
  1135.   j = 10;
  1136.   for (i = 1; i < nstates; i++)
  1137.     {
  1138.       putc(',', ftable);
  1139.  
  1140.       if (j >= 10)
  1141.     {
  1142.       putc('\n', ftable);
  1143.       j = 1;
  1144.     }
  1145.       else
  1146.     {
  1147.       j++;
  1148.     }
  1149.  
  1150.       fprintf(ftable, "%6d", base[i]);
  1151.     }
  1152.  
  1153.   fprintf(ftable, "\n};\n\nstatic const short yypgoto[] = {%6d", base[nstates]);
  1154.  
  1155.   j = 10;
  1156.   for (i = nstates + 1; i < nvectors; i++)
  1157.     {
  1158.       putc(',', ftable);
  1159.  
  1160.       if (j >= 10)
  1161.     {
  1162.       putc('\n', ftable);
  1163.       j = 1;
  1164.     }
  1165.       else
  1166.     {
  1167.       j++;
  1168.     }
  1169.  
  1170.       fprintf(ftable, "%6d", base[i]);
  1171.     }
  1172.  
  1173.   fprintf(ftable, "\n};\n");
  1174.   FREE(base);
  1175. }
  1176.  
  1177.  
  1178. void
  1179. output_table()
  1180. {
  1181.   register int i;
  1182.   register int j;
  1183.  
  1184.   fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
  1185.   fprintf(ftable, "\nstatic const short yytable[] = {%6d", table[0]);
  1186.  
  1187.   j = 10;
  1188.   for (i = 1; i <= high; i++)
  1189.     {
  1190.       putc(',', ftable);
  1191.  
  1192.       if (j >= 10)
  1193.     {
  1194.       putc('\n', ftable);
  1195.       j = 1;
  1196.     }
  1197.       else
  1198.     {
  1199.       j++;
  1200.     }
  1201.  
  1202.       fprintf(ftable, "%6d", table[i]);
  1203.     }
  1204.  
  1205.   fprintf(ftable, "\n};\n");
  1206.   FREE(table);
  1207. }
  1208.  
  1209.  
  1210. void
  1211. output_check()
  1212. {
  1213.   register int i;
  1214.   register int j;
  1215.  
  1216.   fprintf(ftable, "\nstatic const short yycheck[] = {%6d", check[0]);
  1217.  
  1218.   j = 10;
  1219.   for (i = 1; i <= high; i++)
  1220.     {
  1221.       putc(',', ftable);
  1222.  
  1223.       if (j >= 10)
  1224.     {
  1225.       putc('\n', ftable);
  1226.       j = 1;
  1227.     }
  1228.       else
  1229.     {
  1230.       j++;
  1231.     }
  1232.  
  1233.       fprintf(ftable, "%6d", check[i]);
  1234.     }
  1235.  
  1236.   fprintf(ftable, "\n};\n");
  1237.   FREE(check);
  1238. }
  1239.  
  1240.  
  1241.  
  1242. /* copy the parser code into the ftable file at the end.  */
  1243.  
  1244. void
  1245. output_parser()
  1246. {
  1247.   register int c;
  1248. #ifdef DONTDEF
  1249.   FILE *fpars;
  1250. #else
  1251. #define fpars fparser
  1252. #endif
  1253.  
  1254.   if (pure_parser)
  1255.     fprintf(ftable, "#define YYIMPURE 1\n\n");
  1256.   else
  1257.     fprintf(ftable, "#define YYPURE 1\n\n");
  1258.  
  1259. #ifdef DONTDEF    /* JF no longer needed 'cuz open_extra_files changes the
  1260.            currently open parser from bison.simple to bison.hairy */
  1261.   if (semantic_parser)
  1262.     fpars = fparser;
  1263.   else fpars = fparser1;
  1264. #endif
  1265.  
  1266.   /* Loop over lines in the standard parser file.  */
  1267.  
  1268.   while (1)
  1269.     {
  1270.       int write_line = 1;
  1271.  
  1272.       c = getc(fpars);
  1273.  
  1274.       /* See if the line starts with `#line.
  1275.      If so, set write_line to 0.  */
  1276.       if (nolinesflag)
  1277.     if (c == '#')
  1278.       {
  1279.         c = getc(fpars);
  1280.         if (c == 'l')
  1281.           {
  1282.         c = getc(fpars);
  1283.         if (c == 'i')
  1284.           {
  1285.             c = getc(fpars);
  1286.             if (c == 'n')
  1287.               {
  1288.             c = getc(fpars);
  1289.             if (c == 'e')
  1290.               write_line = 0;
  1291.             else
  1292.               fprintf(ftable, "#lin");
  1293.               }
  1294.             else
  1295.               fprintf(ftable, "#li");
  1296.           }
  1297.         else
  1298.           fprintf(ftable, "#l");
  1299.           }
  1300.         else
  1301.           fprintf(ftable, "#");
  1302.       }
  1303.  
  1304.       /* now write out the line... */
  1305.       for ( ; c != '\n' && c != EOF; c = getc(fpars))
  1306.     if (write_line)
  1307.       if (c == '$')
  1308.         {
  1309.           /* `$' in the parser file indicates where to put the actions.
  1310.          Copy them in at this point.  */
  1311.           rewind(faction);
  1312.           for(c=getc(faction);c!=EOF;c=getc(faction))
  1313.         putc(c,ftable);
  1314.         }
  1315.       else
  1316.         putc(c, ftable);
  1317.       if (c == EOF)
  1318.     break;
  1319.       putc(c, ftable);
  1320.     }
  1321. }
  1322.  
  1323.  
  1324. void
  1325. output_program()
  1326. {
  1327.   register int c;
  1328.   extern int lineno;
  1329.  
  1330.   if (!nolinesflag)
  1331.     fprintf(ftable, "#line %d \"%s\"\n", lineno, infile);
  1332.  
  1333.   c = getc(finput);
  1334.   while (c != EOF)
  1335.     {
  1336.       putc(c, ftable);
  1337.       c = getc(finput);
  1338.     }
  1339. }
  1340.  
  1341.  
  1342. void
  1343. free_itemsets()
  1344. {
  1345.   register core *cp,*cptmp;
  1346.  
  1347.   FREE(state_table);
  1348.  
  1349.   for (cp = first_state; cp; cp = cptmp) {
  1350.     cptmp=cp->next;
  1351.     FREE(cp);
  1352.   }
  1353. }
  1354.  
  1355.  
  1356. void
  1357. free_shifts()
  1358. {
  1359.   register shifts *sp,*sptmp;/* JF derefrenced freed ptr */
  1360.  
  1361.   FREE(shift_table);
  1362.  
  1363.   for (sp = first_shift; sp; sp = sptmp) {
  1364.     sptmp=sp->next;
  1365.     FREE(sp);
  1366.   }
  1367. }
  1368.  
  1369.  
  1370. void
  1371. free_reductions()
  1372. {
  1373.   register reductions *rp,*rptmp;/* JF fixed freed ptr */
  1374.  
  1375.   FREE(reduction_table);
  1376.  
  1377.   for (rp = first_reduction; rp; rp = rptmp) {
  1378.     rptmp=rp->next;
  1379.     FREE(rp);
  1380.   }
  1381. }
  1382.